import { Component, OnInit, ViewChild } from '@angular/core';
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import {JqueryTreeComponent} from "../../commons/jquery/jquery.tree.component";

@Component({
  selector: 'app-org',
  templateUrl: './org.component.html',
  styleUrls: ['./org.component.css']
})
export class OrgComponent implements OnInit {

  private treeTestData = [];
  private orgList;
  private selectedOrgList = [];
  private headers = new Headers({'Content-Type': 'application/json'});
  @ViewChild('tree') treeView: JqueryTreeComponent;

  constructor(
    private http: Http
  ) { }

  ngOnInit() {
    this.http.get('assets/tree.json').toPromise().then((res) => {
      this.treeTestData = res.json();
      this.treeView.add(this.treeTestData);
    });
    this.getChildrenOrgList({id: 0});
  }

  getChildrenOrgList(parentData) {
    this.http.post('/api/sys/org/children', JSON.stringify({parentId: parentData.id}), {headers: this.headers})
      .toPromise().then((response) => {
        if(parentData.id > 0){
          parentData.children = response.json();
        } else {
          this.orgList = response.json();
        }
      });
  }

  getSelectedData(data){
    console.log(data)
    for(let obj of this.selectedOrgList){

    }
  }
}
